package org.example.configurature;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

@RestController
@RequestMapping("oauth")
public class TokenController {
    @Autowired
    private TokenStore tokenStore;

    @RequestMapping("logout")
    public Boolean logout(Principal principal) {
        OAuth2Authentication authentication = (OAuth2Authentication) principal;
        OAuth2AccessToken auth2AccessToken = tokenStore.readAccessToken(((OAuth2AuthenticationDetails) authentication.getDetails()).getTokenValue());
        tokenStore.removeAccessToken(auth2AccessToken);
        return true;
    }

    @RequestMapping("me")
    public Principal me(Principal principal) {
        return principal;
    }
}
